package com.hwl.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hwl.cms.feign.OssFileService;
import com.hwl.cms.mapper.AdMapper;
import com.hwl.cms.pojo.Ad;
import com.hwl.cms.pojo.vo.AdVo;
import com.hwl.cms.service.AdService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 广告推荐 服务实现类
 * </p>
 *
 * @author long
 * @since 2021-10-06
 */
@Slf4j
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad> implements AdService {

    @Resource
    private OssFileService ossFileService;

    @Override
    public boolean removeAdImage(String id) {
        Ad ad = baseMapper.selectById(id);
        if (ad != null && !StringUtils.isEmpty(ad.getImageUrl())) {
            Boolean flag = ossFileService.removeFile(ad.getImageUrl()).getSuccess();
            log.info(flag ? "远程调用删除广告图片成功！" : "远程调用删除广告图片失败！");
            return flag;
        }
        return true;
    }

    @Override
    public IPage<AdVo> selectPage(Long page, Long limit) {
        return baseMapper.selectAdPage(new Page<>(page, limit));
    }

    @Override
    public Ad getAdByTitle(String title) {
        return baseMapper.getAdByTitle(title);
    }

    @Cacheable(value = "index", key = "'selectByAdTypeId'")
    @Override
    public List<Ad> selectByAdTypeId(String adTypeId) {
        QueryWrapper<Ad> wrapper = new QueryWrapper<Ad>()
                .orderByAsc("sort", "id").eq("type_id", adTypeId);
        return baseMapper.selectList(wrapper);
    }
}
